package com.dkm.orproject.utils.jwt.intertfaceceptor.domain;

import lombok.Data;

/**
 * @PROJECT_NAME: orproject
 * @DESCRIPTION: 统一返回的结果
 * @USER: 周佳佳
 * @DATE: 2020/7/24 10:19
 */
@Data
public class Result<T> {

    /**
     * 返回的状态码
     */
    private int code;

    /**
     * 结果描述
     */
    private String msg;

    /**
     * 数据
     */
    private T data;



    public Result(int code, String msg) {
        this(code,msg,null);
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public Result(){
    }
    public Result(Code code){
        this(code.getCode(),code.getMsg(),null);
    }

    /**
     * 响应错误的返回结果
     * @param code
     * @param <T>
     * @return
     */
    public static <T> Result<T> fail(Code code) {
        Result<T> result = new Result<>();
        result.setCode(code.getCode());
        result.setMsg(code.getMsg());
        result.setData(null);
        return result;
    }
    /**
     * 响应给成功的返回结果
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> SUCCESS(T data){
        Result<T> result = new Result<>();
        result.setCode(Code.SUCCESS.getCode());
        result.setData(data);
        result.setMsg(Code.SUCCESS.getMsg());
        return result;
    }
}
